package servlet.sample;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


public class SampleServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	protected void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			getBean(Action.class).execute(response.getWriter());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected <T> T getBean(Class<T> type) {
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		return (T) BeanFactoryUtils.beanOfType(context, type);
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		execute(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		execute(request, response);
	}

}
